package com.crp.unitalinforbaseplat.app.sys.controller

import com.crp.unitalinforbaseplat.app.core.Message
import com.crp.unitalinforbaseplat.app.core.MessageUtil
import com.crp.unitalinforbaseplat.app.sys.controller.common.BaseController
import com.crp.unitalinforbaseplat.app.sys.entity.User
import com.crp.unitalinforbaseplat.app.sys.service.RoleService
import com.crp.unitalinforbaseplat.app.sys.service.UserService
import com.crp.unitalinforbaseplat.app.unitalinforbaseplat.entity.PageBean
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.servlet.ModelAndView

@Controller
@RequestMapping("/unitlinforbase/")
class UserController : BaseController(){

    @Autowired
    internal var userService: UserService? = null
    @Autowired
    internal var roleService: RoleService? = null

    // 返回用户列表视图
    @RequestMapping("userlist_index")
    fun userlistIndex(): ModelAndView {
        val modelAndView = ModelAndView("backend/sys/user/userlist_index")
        modelAndView.addObject("userName",getUserName())
        modelAndView.addObject("roleName",getRoleName())
        return modelAndView
    }


    //返回用户列表
    @RequestMapping("/userlist")
    @ResponseBody
    fun list(userlist: User): PageBean<User> {
        val pageBean = PageBean<User>()
        pageBean.setParameter(userlist)
        return userService!!.list(pageBean)
    }

    /**
     *
     * delete user
     * written by jackBin
     */
    @RequestMapping("/delete")
    @ResponseBody
    fun delete(id:Int): Message {
        return userService!!.deleteById(id)
    }

    /**
     * 返回带有实体类User的视图
     * jackBin
     */
    @RequestMapping("edit_index")
    @ResponseBody
    fun editIndex(id:Int): ModelAndView {
        val modelAndView = ModelAndView("backend/sys/user/edit_index")
        modelAndView.addObject("user",userService!!.selectById(id))
        return modelAndView
    }

    /**
     * 编辑用户方法
     * jackBin
     */
    @RequestMapping("/edit")
    @ResponseBody
    fun edit(user: User):Message{
        return userService!!.edit(user)
    }

    /**
     *返回add视图
     *jackBin
     */
    @RequestMapping("/add_index")
    @ResponseBody
    fun addIndex(): ModelAndView
    {
        val modelAndView = ModelAndView("backend/sys/user/add_index")
        return modelAndView
    }

    /**
     *新增用户
     *jackBin
     */
    @RequestMapping("/add")
    @ResponseBody
    fun saveOrUpdate(user: User):Message
    {
        return userService!!.add(user)
    }

    @RequestMapping("/register")
    @ResponseBody
    internal fun register(user: User): Message {
        user.userId = user.userName
        userService!!.add(user)
//        val l =Long[]
//        roleService!!.roleAddUser(user.id.toLong(), 5);
        return MessageUtil.createMessage(true, "注册成功")
    }
}
